package com.seckill.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import com.seckill.util.ValidatorUtil;
import org.apache.commons.lang3.StringUtils;

/**
 * 校验手机号格式
 *
 * @author yjw
 */
public class IsMobileValidator implements ConstraintValidator<IsMobile, String> {

    private boolean required = false;

    @Override
    public void initialize(IsMobile constraintAnnotation) {
        required = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StringUtils.isEmpty(value)) {
            return false;
        }
        if (required) {
            //必填
            return ValidatorUtil.isMobile(value);
        }
        return false;
    }


}
